<?php
class Server {
  public static function get_objs($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));

    $categories = $orm->get('pb_ht_category', NULL, NULL, NULL, NULL, NULL)->d;

    $root = new stdClass();
    $root->id = 0;
    $root->name = 'ROOT';
    $root->parent_id = -1;
    Server::__create_root($categories, $root);
    
    $rslt = new stdClass();
    $rslt->data = array();
    Server::__get_all($root, $req->cat, $rslt, false);
    
    
    $opts = new stdClass(); 
    $opts->cd = 't.title LIKE :v0';
    $opts->cdv = array(':v0' => '%' . $req->search . '%');
    if ($req->cat != 0) {
      $opts->cd .= ' AND (';
      foreach ($rslt->data as $idx => &$itm) {
        $opts->cd .= "t.category_id = :v" . ($idx + 1);
        $opts->cdv[":v" . ($idx + 1)] = "$itm";
        if ($idx < count($rslt->data)  -1) {
          $opts->cd .= ' OR ';
        }
      }
      $opts->cd .= ')';
    }
    
    $data = $orm->get('pb_ht_article', $req->o, $req->q, $req->pg, SIZE, $opts);
    echo json_encode($data);
  }
  
  public static function delete_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 'article_track_id = :v1';
    $opts->cdv = array(':v1' => $req->track_id);
    $orm->delete_by_opts('pb_ht_article_file', $opts);
    
    $orm->delete('pb_ht_article', $req);
    if (isset($req->__pg)) {
      $categories = $orm->get('pb_ht_category', NULL, NULL, NULL, NULL, NULL)->d;

      $root = new stdClass();
      $root->id = 0;
      $root->name = 'ROOT';
      $root->parent_id = -1;
      Server::__create_root($categories, $root);
      
      $rslt = new stdClass();
      $rslt->data = array();
      Server::__get_all($root, $req->__cat, $rslt, false);
      
      
      $opts = new stdClass(); 
      $opts->cd = 't.title LIKE :v0';
      $opts->cdv = array(':v0' => '%' . $req->__search . '%');
      if ($req->__cat != 0) {
        $opts->cd .= ' AND (';
        foreach ($rslt->data as $idx => &$itm) {
          $opts->cd .= "t.category_id = :v" . ($idx + 1);
          $opts->cdv[":v" . ($idx + 1)] = "$itm";
          if ($idx < count($rslt->data)  -1) {
            $opts->cd .= ' OR ';
          }
        }
        $opts->cd .= ')';
      }
      
      $data = $orm->get('pb_ht_article', $req->__o, $req->__q, $req->__pg, SIZE, $opts);
      echo json_encode($data);
    }
  }
  
  public static function __get_all($node, $id, $rslt, $is_to_add) {
    if ($node->id == $id || $is_to_add) {
      $rslt->data[] = $node->id;
    }
    foreach ($node->__c as &$itm) {
      Server::__get_all($itm, $id, $rslt, $is_to_add || $node->id == $id);
    }
  }
  
  public static function __create_root($arr, $node) {
    for ($i = 0, $il = count($arr); $i < $il; $i++) {
      $itm = $arr[$i];
      if ($itm->parent_id == $node->id) {
        $itm->__c = array();
        $node->__c[] = $itm;
        Server::__create_root($arr, $itm);
      }
    }
  }
}
?>